"""插件配置"""

from datetime import time
from typing import List, Optional
from pydantic import BaseModel, Field, validator
from nonebot import get_driver


class Config(BaseModel):
    """插件配置类"""
    
    # 核心功能
    message_counter_track_bot: bool = Field(default=False, description="是否统计机器人自己发送的消息")
    
    # 排行榜设置
    message_counter_default_limit: int = Field(default=20, description="排行榜默认显示人数")
    message_counter_show_percentage: bool = Field(default=True, description="是否显示消息占比")
    message_counter_show_time: bool = Field(default=True, description="是否显示生成时间")
    message_counter_hidden_users: List[str] = Field(default_factory=list, description="隐藏的用户ID列表")
    message_counter_hidden_groups: List[str] = Field(default_factory=list, description="隐藏的群组ID列表")
    
    # 图片生成
    message_counter_enable_image: bool = Field(default=True, description="是否启用图片生成")
    message_counter_image_format: str = Field(default="png", description="图片格式")
    message_counter_chart_width: int = Field(default=1080, description="图表宽度")
    message_counter_chart_height: int = Field(default=720, description="图表高度")
    
    # 自动推送
    message_counter_auto_push: bool = Field(default=False, description="是否启用自动推送")
    message_counter_auto_push_enable: bool = Field(default=False, description="是否启用自动推送排行榜")
    message_counter_auto_push_time: time = Field(default=time(20, 0), description="自动推送时间")
    message_counter_auto_push_groups: List[str] = Field(default_factory=list, description="自动推送群组列表")
    message_counter_push_groups: List[str] = Field(default_factory=list, description="推送群组列表")
    message_counter_daily_push: bool = Field(default=True, description="每日推送昨日排行榜")
    message_counter_weekly_push: bool = Field(default=False, description="每周推送上周排行榜")
    message_counter_monthly_push: bool = Field(default=False, description="每月推送上月排行榜")
    message_counter_yearly_push: bool = Field(default=False, description="每年推送去年排行榜")
    message_counter_push_times: List[str] = Field(default_factory=list, description="其他推送时间点")
    
    # 龙王禁言
    message_counter_enable_mute: bool = Field(default=False, description="是否启用龙王禁言")
    message_counter_dragon_king_enable: bool = Field(default=False, description="是否启用龙王系统")
    message_counter_dragon_king_check_time: time = Field(default=time(21, 0), description="龙王检查时间")
    message_counter_mute_duration: int = Field(default=1, description="禁言时长（天）")
    message_counter_mute_groups: List[str] = Field(default_factory=list, description="禁言群组列表")
    
    # 数据清理
    message_counter_cleanup_enable: bool = Field(default=True, description="是否启用数据清理")
    
    # 数据库
    message_counter_database_url: Optional[str] = Field(default=None, description="数据库连接URL")
    
    @validator('message_counter_auto_push_time', pre=True)
    def parse_auto_push_time(cls, v):
        """解析自动推送时间"""
        if isinstance(v, str):
            try:
                hour, minute = map(int, v.split(':'))
                return time(hour, minute)
            except (ValueError, AttributeError):
                return time(20, 0)  # 默认20:00
        return v
    
    @validator('message_counter_dragon_king_check_time', pre=True)
    def parse_dragon_king_check_time(cls, v):
        """解析龙王检查时间"""
        if isinstance(v, str):
            try:
                hour, minute = map(int, v.split(':'))
                return time(hour, minute)
            except (ValueError, AttributeError):
                return time(21, 0)  # 默认21:00
        return v
    
    class Config:
        extra = "ignore"


# 获取配置实例
driver = get_driver()
config = Config.parse_obj(driver.config.dict())